using System;
using System.Collections;
using System.Data;
using PCSComUtils.PCSExc;

using PCSComUtils.Common;
using PCSComProduction.WorkOrder.DS;
using System.Transactions;
using PCSComUtils.DataContext;
using PCSComUtils.DataAccess;
using System.Linq;


namespace PCSComProduction.WorkOrder.BO
{
    public class ManufacturingCloseBO //: IManufacturingCloseBO
    {
        public ManufacturingCloseBO()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        #region IObjectBO Members
        /// <summary>
        /// Update status to ManufacturingClose and MfgCloseDate of each WorkOrderLine in ArrayList
        /// </summary>
        /// <param name="pdtmCloseDate">Close Date</param>
        /// <param name="parrSelectedLines">All selected work order line to be closed</param>
        /// <author>Trada</author>
        /// <date>Friday, June </date>
        //[AutoComplete(true)]
        public void CloseWorkOrderLines(DateTime pdtmCloseDate, ArrayList parrSelectedLines)
        {
            using (var trans = new TransactionScope())
            {
                using (var db = new PCSDataContext(Utils.Instance.ConnectionString))
                {
                    try
                    {
                        foreach (int iWODetailID in parrSelectedLines)
                        {
                            var obj = db.PRO_WorkOrderDetails.SingleOrDefault<PRO_WorkOrderDetail>(e => e.WorkOrderDetailID == iWODetailID);
                            obj.MfgCloseDate = pdtmCloseDate;
                            obj.Status = (byte)WOLineStatus.MfgClose;
                            db.SubmitChanges();
                        }
                    }
                    catch (PCSDBException ex) { throw ex; }
                    catch (Exception ex) { throw ex; }
                    finally
                    { trans.Complete(); }

                }
            }
        }
        /// <summary>
        /// CloseFinanceWorkOrderLines
        /// </summary>
        /// <param name="pdtmCloseDate"></param>
        /// <param name="parrSelectedLines"></param>
        //[AutoComplete(true)]
        public void CloseFinanceWorkOrderLines(DateTime pdtmCloseDate, ArrayList parrSelectedLines)
        {
            using (var trans = new TransactionScope())
            {
                using (var db = new PCSDataContext(Utils.Instance.ConnectionString))
                {
                    try
                    {
                        foreach (int iWODetailID in parrSelectedLines)
                        {
                            var obj = db.PRO_WorkOrderDetails.SingleOrDefault<PRO_WorkOrderDetail>(e => e.WorkOrderDetailID == iWODetailID);
                            obj.MfgCloseDate = pdtmCloseDate;
                            obj.Status = (byte)WOLineStatus.FinClose;
                            db.SubmitChanges();
                        }
                    }
                    catch (PCSDBException ex) { throw ex; }
                    catch (Exception ex) { throw ex; }
                    finally
                    { trans.Complete(); }
                }
            }
        }

        /// <summary>
        /// - Search all work order line have status Released and have Due Date between FromDueDate and ToDueDate
        /// </summary>
        /// <param name="pdtmFromDueDate">From due date of work order line</param>
        /// <param name="pdtmToDueDate">To due date of work order line</param>
        /// <returns>All work order line have status is Released</returns>
        /// <author>Trada</author>
        /// <date>Friday, June 10 2005</date>
        //[AutoComplete(true)]
        public DataSet SearchManufacturingCloseWO(int pintCCNID, int pintMasterLocationID, DateTime pdtmFromDueDate, DateTime pdtmToDueDate)
        {
            try
            {
                dsPRO_WorkOrderDetail = new PRO_WorkOrderDetailDS();
                return dsPRO_WorkOrderDetail.SearchWOForClose(WOLineStatus.MfgClose, pintCCNID, pintMasterLocationID, pdtmFromDueDate, pdtmToDueDate);
            }
            catch (PCSDBException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }

        //[AutoComplete(true)]
        public DataSet SearchReleasedWO(int pintCCNID, int pintMasterLocationID, DateTime pdtmFromDueDate, DateTime pdtmToDueDate)
        {
            try
            {
                dsPRO_WorkOrderDetail = new PRO_WorkOrderDetailDS();
                return dsPRO_WorkOrderDetail.SearchWOForClose(WOLineStatus.Released, pintCCNID, pintMasterLocationID, pdtmFromDueDate, pdtmToDueDate);
            }
            catch (PCSDBException ex)
            {
                throw ex;
            }
            catch (Exception ex)
            {
                throw ex;
            }

        }
        //[AutoComplete(true)]
        public void UpdateDataSet(System.Data.DataSet dstData)
        {
            // TODO:  Add ManufacturingCloseBO.UpdateDataSet implementation
        }

        //[AutoComplete(true)]
        public void Update(object pObjectDetail)
        {
            // TODO:  Add ManufacturingCloseBO.Update implementation
        }

        //[AutoComplete(true)]
        public void Delete(object pObjectVO)
        {
            // TODO:  Add ManufacturingCloseBO.Delete implementation
        }

        //[AutoComplete(true)]
        public void Add(object pObjectDetail)
        {
            // TODO:  Add ManufacturingCloseBO.Add implementation
        }

        //[AutoComplete(true)]
        public object GetObjectVO(int pintID, string VOclass)
        {
            // TODO:  Add ManufacturingCloseBO.GetObjectVO implementation
            return null;
        }

        #endregion
        private DS.PRO_WorkOrderDetailDS dsPRO_WorkOrderDetail;
        private DS.PRO_WorkOrderMasterDS dsPRO_WorkOrderMaster;
        private DS.PRO_WorkOrderCompletionDS dsPRO_WorkOrderCompletion;
        //private ScrapRecording.DS.PRO_OperationScrapMasterDS dsPRO_OperationScrapMaster;


        //private ScrapRecording.DS.PRO_AssemblyScrapMasterDS dsPRO_AssemblyScrapMaster;


    }
}
